"""Add calendar component tables

Revision ID: f3754d370ede
Revises: 
Create Date: 2025-07-31 22:07:10.775676

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy import Enum, Integer, String, Text, DateTime, Boolean, ForeignKey
from sqlalchemy.sql import func


# revision identifiers, used by Alembic.
revision: str = 'f3754d370ede'
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # 创建日历表
    op.create_table('calendars',
        sa.Column('id', Integer, primary_key=True, autoincrement=True),
        sa.Column('name', String(100), nullable=False, comment='日历名称'),
        sa.Column('description', Text, comment='日历描述'),
        sa.Column('color', String(7), default='#10B981', comment='日历颜色'),
        sa.Column('is_default', Boolean, default=False, comment='是否默认日历'),
        sa.Column('is_shared', Boolean, default=False, comment='是否共享'),
        sa.Column('created_at', DateTime, default=func.now(), comment='创建时间'),
        sa.Column('updated_at', DateTime, default=func.now(), onupdate=func.now(), comment='更新时间'),
        sa.Column('created_by', String(100), comment='创建者')
    )
    
    # 创建事件表
    op.create_table('calendar_events',
        sa.Column('id', Integer, primary_key=True, autoincrement=True),
        sa.Column('title', String(255), nullable=False, comment='日程标题'),
        sa.Column('description', Text, comment='日程描述'),
        sa.Column('event_date', DateTime, nullable=False, comment='日程日期'),
        sa.Column('end_date', DateTime, comment='结束日期'),
        sa.Column('event_type', String(20), default='normal', comment='日程类型'),
        sa.Column('priority', String(20), default='medium', comment='优先级'),
        sa.Column('is_all_day', Boolean, default=False, comment='是否全天日程'),
        sa.Column('location', String(255), comment='地点'),
        sa.Column('reminder_minutes', Integer, default=15, comment='提前提醒分钟数'),
        sa.Column('is_recurring', Boolean, default=False, comment='是否重复'),
        sa.Column('recurrence_pattern', String(20), default='none', comment='重复模式'),
        sa.Column('recurrence_end_date', DateTime, comment='重复结束日期'),
        sa.Column('color', String(7), default='#3B82F6', comment='日程颜色'),
        sa.Column('is_completed', Boolean, default=False, comment='是否完成'),
        sa.Column('is_private', Boolean, default=False, comment='是否私有'),
        sa.Column('created_at', DateTime, default=func.now(), comment='创建时间'),
        sa.Column('updated_at', DateTime, default=func.now(), onupdate=func.now(), comment='更新时间'),
        sa.Column('created_by', String(100), comment='创建者'),
        sa.Column('calendar_id', Integer, ForeignKey('calendars.id'), comment='所属日历'),
        sa.Column('parent_event_id', Integer, ForeignKey('calendar_events.id'), comment='父事件ID（用于重复事件）')
    )
    
    # 创建事件参与者表
    op.create_table('event_attendees',
        sa.Column('id', Integer, primary_key=True, autoincrement=True),
        sa.Column('event_id', Integer, ForeignKey('calendar_events.id'), nullable=False, comment='事件ID'),
        sa.Column('user_id', String(100), comment='用户ID'),
        sa.Column('user_name', String(100), comment='用户名'),
        sa.Column('user_email', String(255), comment='用户邮箱'),
        sa.Column('status', String(20), default='pending', comment='参与状态（pending, accepted, declined, tentative）'),
        sa.Column('role', String(20), default='attendee', comment='角色（attendee, organizer, optional）'),
        sa.Column('created_at', DateTime, default=func.now(), comment='创建时间')
    )
    
    # 创建索引
    op.create_index('idx_calendar_events_event_date', 'calendar_events', ['event_date'])
    op.create_index('idx_calendar_events_calendar_id', 'calendar_events', ['calendar_id'])
    op.create_index('idx_event_attendees_event_id', 'event_attendees', ['event_id'])


def downgrade() -> None:
    """Downgrade schema."""
    # 删除索引
    op.drop_index('idx_event_attendees_event_id')
    op.drop_index('idx_calendar_events_calendar_id')
    op.drop_index('idx_calendar_events_event_date')
    
    # 删除表
    op.drop_table('event_attendees')
    op.drop_table('calendar_events')
    op.drop_table('calendars')
